#!/usr/bin/env sh

# Resolve links: \$0 may be a link
app_path=\$0

# Need this for daisy-chained symlinks.
while
    APP_HOME=\${app_path%"\${app_path##*/}"}  # leaves a trailing /; empty if no leading path
    [ -h "\$app_path" ]
do
    ls=\$( ls -ld "\$app_path" )
    link=\${ls#*' -> '}
    case \$link in             #(
      /*)   app_path=\$link ;; #(
      *)    app_path=\$APP_HOME\$link ;;
    esac
done

# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=\${0##*/}
APP_HOME=\$( cd "\${APP_HOME:-./}${appHomeRelativePath}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and ${optsEnvironmentVar} to pass JVM options to this script.
DEFAULT_JVM_OPTS=${defaultJvmOpts}

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "\$( uname )" in               #(
  CYGWIN* )         cygwin=true  ;; #(
  Darwin* )         darwin=true  ;; #(
  MSYS* | MINGW* )  msys=true    ;; #(
  NONSTOP* )        nonstop=true ;;
esac

CLASSPATH="\$APP_HOME/lib/*"
JAVA_HOME="\$APP_HOME"
JAVACMD="\$JAVA_HOME/bin/java"

# For Cygwin or MSYS, switch paths to Windows format before running java
if "\$cygwin" || "\$msys" ; then
    APP_HOME=\$( cygpath --path --mixed "\$APP_HOME" )
    CLASSPATH=\$( cygpath --path --mixed "\$CLASSPATH" )
    JAVACMD=\$( cygpath --unix "\$JAVACMD" )

    # Now convert the arguments - kludge to limit ourselves to /bin/sh
    for arg do
        if
            case \$arg in                                #(
              -*)   false ;;                            # don't mess with options #(
              /?*)  t=\${arg#/} t=/\${t%%/*}              # looks like a POSIX filepath
                    [ -e "\$t" ] ;;                      #(
              *)    false ;;
            esac
        then
            arg=\$( cygpath --path --ignore --mixed "\$arg" )
        fi
        # Roll the args list around exactly as many times as the number of
        # args, so each arg winds up back in the position where it started, but
        # possibly modified.
        #
        # NB: a `for` loop captures its iteration list before it begins, so
        # changing the positional parameters here affects neither the number of
        # iterations, nor the values presented in `arg`.
        shift                   # remove old arg
        set -- "\$@" "\$arg"      # push replacement arg
    done
fi

# Escape application args
save () {
    for i do printf %s\\\\n "\$i" | sed "s/'/'\\\\\\\\''/g;1s/^/'/;\\\$s/\\\$/' \\\\\\\\/" ; done
    echo " "
}
APP_ARGS=\$(save "\$@")

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- \$JAVA_TOOL_OPTIONS \$DEFAULT_JVM_OPTS \$CDS_JVM_OPTS \$JAVA_OPTS \$${optsEnvironmentVar} <% if ( appNameSystemProperty ) { %>"\"-D${appNameSystemProperty}=\$APP_BASE_NAME\"" <% } %>-classpath "\"\$CLASSPATH\"" ${mainClassName} "\$APP_ARGS"

# Unset this environment variable before calling Java to prevent it from appearing in stderr.
# Instead, the value stored in this variable is passed in to the command in the previous line manually.
unset JAVA_TOOL_OPTIONS

<% if ( System.properties['BADASS_RUN_IN_BIN_DIR'] ) { %>cd "\$APP_HOME/bin" && <% } %>exec "\$JAVACMD" "\$@"
